package cn.hl.hm.controller;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;
import com.thoughtworks.xstream.io.xml.XppDriver;

import cn.hl.hm.constants.PayMethod;
import cn.hl.hm.entity.UnifiedOrderRequest;
import cn.hl.hm.entity.UnifiedOrderRespose;
import cn.hl.hm.handler.PayExecuteFactory;
import cn.hl.hm.handler.PayHandler;
/**
 * 
 * @author 大和
 *
 */
@Controller
@RequestMapping("/wxScanCallbackController")
public class WxScanCallbackController {


	@RequestMapping(value = "/callback")
	public String callback(HttpServletRequest request, HttpServletResponse response) {
		UnifiedOrderRespose unifiedOrderRespose = new UnifiedOrderRespose();
		XStream xStream = new XStream(new XppDriver(new XmlFriendlyNameCoder("_-", "_"))); // <span
		String returnResult = null;
		try {
			PrintWriter writer = response.getWriter();
			PayHandler payHandler = PayExecuteFactory.findPayHandler(PayMethod.ROAD_WX_SCAN_MER);
			Boolean result = payHandler.callback(request,response);

			if (result) {
				unifiedOrderRespose.setReturn_code("SUCCESS");
				unifiedOrderRespose.setReturn_msg("OK");
				xStream.alias("xml", UnifiedOrderRequest.class);// 根元素名需要是xml
				returnResult = xStream.toXML(unifiedOrderRespose);
				writer.write(returnResult);
			} else {
				unifiedOrderRespose.setReturn_code("FAIL");
				unifiedOrderRespose.setReturn_msg("未获取到微信返回的结果");
				xStream.alias("xml", UnifiedOrderRequest.class);// 根元素名需要是xml
				returnResult = xStream.toXML(unifiedOrderRespose);
				writer.write(returnResult);
			}
			if (writer != null) {
				writer.close();
			}
			return null;
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}
}
